home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / MISC / DTMFF110 / REALFFTA.ASM < prev    next >
Assembly Source File  |  1996-03-10  |  5KB  |  213 lines

  1.     IDEAL
  2.     NOJUMPS
  3.     SMART
  4.  
  5.     P386                    ; Use 386 instructions
  6.  
  7.     MODEL   medium
  8.  
  9.     EXTRN   _Points:word, _Passes:word, _BitReversed:word, _SinTable:word
  10.  
  11.     CODESEG
  12.  
  13. ;
  14. ; void RealFFT(int near *DataTable);
  15. ;
  16. PROC    C RealFFT
  17.     PUBLIC  RealFFT
  18.     ARG     DataTable:word
  19.     LOCAL   sptr:word,But_per_group:word,twiddle:word
  20.     LOCAL   Endptr1:word,Endptr2:word,Sval:dword,Cval:dword
  21.     LOCAL   HRplus:dword,HRminus:dword,HIplus:dword,HIminus:dword
  22.     USES    SI,DI
  23.  
  24.     mov     ax,[DataTable]
  25.     mov     bx,[_Points]
  26.     shl     bx,1
  27.     add     ax,bx
  28.     mov     [Endptr1],ax            ; Point to end of buffer (begin+points*2)
  29.  
  30.     shr     bx,1                    ; Butterflies for 1st pass=(Points/4) (*4 for indexing pairs of ints)
  31.  
  32. ;
  33. ; Beginning of a new pass through the data
  34. ;
  35.  
  36. Pass:
  37.     mov     si,[DataTable]
  38.     mov     di,si
  39.     add     di,bx
  40.     mov     [But_per_group],bx      ; save this value for later
  41.  
  42.     mov     bx,[_SinTable]
  43.     mov     [sptr],bx
  44.  
  45. ;
  46. ; Beginning of a new group (groups all use the same sin/cos values)
  47. ;
  48.  
  49. Set:
  50.     mov     bx,[sptr]
  51.     add     [sptr],4
  52.     movsx   ecx,[word ptr bx]       ; sin is always in ECX
  53.     movsx   edx,[word ptr bx+2]     ; cos is always in EDX
  54.  
  55.     mov     [Endptr2],di            ; End pointer for this group
  56.  
  57. ;
  58. ; Beginning of a Butterfly
  59. ;
  60.  
  61. Butterfly:
  62.     movsx   eax,[word ptr di]       ; Get Br
  63.     movsx   ebx,[word ptr di+2]     ; Get Bi
  64.     imul    eax,edx
  65.     imul    ebx,ecx
  66.     add     eax,ebx
  67.     movsx   ebx,[word ptr si]       ; Get Ar
  68.     sar     eax,15                  ; v1 = Br*cos + Bi*sin
  69.     sub     ebx,eax
  70.     sar     ebx,1
  71.     mov     [si],bx                 ; Ar = Ar - v1
  72.     add     ebx,eax
  73.     movsx   eax,[word ptr di]       ; Get Br
  74.     mov     [word ptr di],bx        ; Br = Ar + v1
  75.  
  76.     movsx   ebx,[word ptr di+2]     ; Get Bi
  77.     imul    eax,ecx
  78.     imul    ebx,edx
  79.     sub     eax,ebx
  80.     movsx   ebx,[word ptr si+2]     ; Get Ai
  81.     sar     eax,15                  ; v2 = Br*sin - Bi*cos
  82.     sub     ebx,eax
  83.     sar     ebx,1
  84.     mov     [di+2],bx               ; Bi = Ai - v2
  85.     add     ebx,eax
  86.     mov     [si+2],bx               ; Ai = Ai + v2
  87.  
  88.     add     si,4
  89.     add     di,4
  90.  
  91. ;
  92. ;  End of Butterfly
  93. ;
  94.  
  95.     cmp     si,[Endptr2]
  96.     jb      short Butterfly
  97.  
  98.     mov     si,di
  99.     add     di,[But_per_group]
  100.  
  101. ;
  102. ;  End of Group
  103. ;
  104.  
  105.     cmp     si,[Endptr1]
  106.     jb      short Set
  107.  
  108. ;
  109. ;  End of Pass
  110. ;
  111.  
  112.     mov     bx,[But_per_group]
  113.     shr     bx,1
  114.     cmp     bx,2
  115.     jne     Pass
  116.     
  117. ;
  118. ;  Reconstruct the output for the real input
  119. ;
  120.  
  121.     mov     bx,[DataTable]
  122.     movsx   eax,[word ptr bx]       ; Handle bin 0 case special
  123.     movsx   ecx,[word ptr bx+2]
  124.     add     eax,ecx
  125.     mov     [bx],ax
  126.     mov     [word ptr bx+2],0
  127.  
  128.     mov     si,[_BitReversed]       ; Set up index pointers
  129.     add     si,2
  130.     mov     di,[_Points]
  131.     add     di,si
  132.     sub     di,4
  133. Loopit:
  134.     mov     bx,[di]                 ; Point to Hr(N-n)
  135.     add     bx,bx                   ; Double it for integers
  136.     add     bx,[DataTable]
  137.  
  138.     movsx   eax,[word ptr bx]       ; Get Hr(N-n)
  139.     movsx   ecx,[word ptr bx+2]     ; Get Hi(N-n)
  140.  
  141.     mov     bx,[si]                 ; Point to Hr(n)
  142.     add     bx,bx                   ; Double it for integers
  143.  
  144.     add     bx,[_SinTable]
  145.     movsx   edx,[word ptr bx]
  146.     mov     [Sval],edx
  147.     movsx   edx,[word ptr bx+2]
  148.     mov     [Cval],edx
  149.  
  150.     sub     bx,[_SinTable]
  151.     add     bx,[DataTable]
  152.     movsx   edx,[word ptr bx+2]     ; Get Hi(n)
  153.  
  154.     sub     edx,ecx                 ; edx=Hi(n)-Hi(N-n)
  155.     sal     ecx,1
  156.     add     ecx,edx                 ; ecx=Hi(n)+Hi(N-n)
  157.  
  158.     mov     [HIminus],edx
  159.     mov     [HIplus],ecx
  160.  
  161.     movsx   edx,[word ptr bx]       ; Get Hr(n)
  162.  
  163.     sub     edx,eax                 ; edx=Hr(n)-Hr(N-n)
  164.     sal     eax,1
  165.     add     eax,edx                 ; eax=Hr(n)+Hr(N-n)
  166.  
  167.     mov     [HRminus],edx
  168.  
  169.     imul    ecx,[Cval]              ; Multiply Hi+ by (-COS)
  170.     imul    edx,[Sval]              ; Multiply Hr- by (-SIN)
  171.     sub     ecx,edx
  172.     sar     ecx,15
  173.  
  174.     sub     eax,ecx                 ; eax=(Hr+) - (Hi+)(-COS) + (Hr-)(-SIN)
  175.     sar     eax,1                   ;           *0.5
  176.     add     ecx,eax                 ; ecx=(Hr+) + (Hi+)(-COS) - (Hr-)(-SIN)
  177.                     ;           *0.5
  178.  
  179.     mov     [bx],ax                 ; Save Fr(n)
  180.  
  181.     mov     eax,[HIplus]
  182.     imul    eax,[Sval]
  183.  
  184.     mov     edx,[HRminus]
  185.     imul    edx,[Cval]
  186.     add     eax,edx
  187.     sar     eax,15
  188.  
  189.     mov     edx,[HIminus]
  190.  
  191.     sub     eax,edx                 ; eax=(-(Hi-) + (Hr-)(-COS) + (Hi+)(-SIN))
  192.     sar     eax,1                   ;            *0.5
  193.     add     edx,eax                 ; edx=( (Hi-) + (Hr-)(-COS) + (Hi+)(-SIN)) *0.5
  194.  
  195.     mov     [bx+2],dx               ; Save Fi(n)
  196.  
  197.     mov     bx,[di]                 ; Point to Hr(N-n)
  198.     add     bx,bx                   ; Double it for integers
  199.     add     bx,[DataTable]
  200.     mov     [bx],cx                 ; Save Fr(N-n)
  201.     mov     [bx+2],ax               ; Save Fi(N-n)
  202.  
  203.     add     si,2
  204.     sub     di,2
  205.     cmp     si,di
  206.     jbe     Loopit
  207.  
  208.     ret
  209.  
  210. ENDP    RealFFT
  211.  
  212.     end
  213.